<template>
	<view>
		<view class="goods_list">
			<view class="goods_nav">
				<p @tap="nav(v.id)" v-for="(v,i) in navList" :key="i" :class="{'goods_nav_act':categoryId == v.id}">{{v.name}} </p>
			</view>
			<view class="goods_main bgw">
				<view @tap="GoGoods(v)" v-for="(v,i) in goodsList" :key="i">
					<image class="goodsImg" :src="v.thumbnail" mode="aspectFill"></image>
					<view class="goods_txt">
						<p class=" name ">
							{{v.name}}
						</p>
						<view @tap.stop class="bottom">
							<span>￥</span>
							{{v.price}}
							<view class="fr">
								<!-- <image class="reduce" src="../../static/image/icon_jian@2x.png" mode=""></image> -->
								<image @tap="add(v.id)" class="add" src="../../static/image/jiaAddProduct.png" mode=""></image>
							</view>
						</view>
					</view>
				</view>
				<view v-if="goodsList == '' " class="noneProduct">
					<image src="../../static/image/shangpin@2x.png" mode=""></image>
					<br />
					<p>暂无商品</p>
				</view>
			</view>
		</view>
		<view class="goods_sum  bt1 bgw">
			<view class="fl">
				<image @tap="carShows()" src="../../static/image/che.png" mode=""></image>
				<span class="sums">
					{{cartMoneys.count}}
				</span>
			</view>
			<view class="fl">
				<p class="money">
					￥{{cartMoneys.priceAfterDiscount}}
				</p>
				<p v-if="cartMoneys.activity" class="p2">
					满{{cartMoneys.activity.exceedPrice}} 减
					{{cartMoneys.activity.deductionPrice}}
				</p>
			</view>
			<view class="fr">
				<u-button @tap="checkoutOrder()" type="success">去结算({{cartMoneys.count}})</u-button>
			</view>
		</view>
		<view v-show="carShow" @tap="carShow = false" @tap.stop class="models">
			<ul class="modelsGoodsList">
				<li v-for="(v,i) in cartList" :key="i" class="bb">
					<image class="goodsImg" :src="v.productInfo.thumbnail" mode=""></image>
					<view class="goods_txt">
						<p class=" name two">
							{{v.productInfo.name}}
						</p>
						<view @tap.stop class="bottom">
							{{v.productInfo.price}}
							<view class="fr">
								<image @tap="updaCart('reduce',i,v.id,v.productInfo.quantity)" class="reduce" src="../../static/image/icon_jian@2x.png"
								 mode=""></image>
								<span>{{v.productInfo.quantity}}</span>
								<image @tap="updaCart('add',i,v.id,v.productInfo.quantity)" class="add" src="../../static/image/icon_jia@2x.png"
								 mode=""></image>
							</view>
						</view>
					</view>
				</li>
			</ul>
		</view>
	</view>
</template>
<script>
	import {
		productList,
		storeCalculatePrice,
		orderStoreCheckout,
		cartAdd,
		cartListStoreList,
		cartupdate,
		cartDelete,
		orderCheckout
	} from '@/api/api.js'
	import {
		toLogin
	} from '@/tool/index.js'
	export default {
		data() {
			return {
				carShow: false,
				isCreator: false,
				storeId: 0,
				id: 0,
				categoryId: null,
				car: [],
				cartMoneys: [],
				navList: [],
				goodsList: [],
				cartList: [],
				stops: false,
				pageable: {
					pageNumber: 1,
					pageSize: 10
				}
			}
		},
		created() {},
		methods: {
			checkoutOrder() {
				let token = uni.getStorageSync('token')
				if (!token) {
					toLogin()
					return
				}
				if (this.isCreator) {
					this.$u.toast(`自己店铺不允许买卖`);
					return
				}
				if (this.cartMoneys.count != 0) {
					this.checkingOrder()
				} else {
					this.$u.toast(`请选择商品`);
				}
			},
			async checkingOrder() {
				let selectArr = []
				for (let i = 0; i < this.cartList.length; i++) {
					selectArr.push(this.cartList[i].id)
				}
				var url = `../CreatedOrder/CreatedOrder?cartIds=${selectArr}`
				let data = await orderCheckout({
					cartIds:selectArr
				})
				if (data.code == 0) { 
					this.$navigateTo(url)
				}
			},
			async cartDeletes(i, cartId) {
				let data = await cartDelete({
					cartIds: cartId
				})
				if (data.code == 0) {
					this.cartList.splice(i, 1)
					this.checkoutPrice()
				}
			},
			async updaCart(type, i, cartId, quantity) {
				if (type == 'reduce') {
					if (quantity == 1) {
						this.cartDeletes(i, cartId)
						return
					} else {
						quantity = quantity - 1
					}
				}
				if (type == 'add') {
					quantity = quantity + 1
				}
				let data = await cartupdate({
					cartId: cartId,
					quantity: quantity
				})
				if (data.code == 0) {
					this.cartList[i].productInfo.quantity = quantity
					this.checkoutPrice()
				}
			},
			async add(id) {
				let token = uni.getStorageSync('token')
				if (!token) {
					toLogin()
					return
				}
				if (this.isCreator) {
					this.$u.toast(`自己店铺不允许买卖`);
					return
				}
				let data = await cartAdd({
					productId: id,
					quantity: 1
				})
				if (data.code == 0) {
					this.checkoutPrice()
					this.getCartList()
				}
			},
			// 获取店铺内 购物车 列表|
			async getCartList() {
				let data = await cartListStoreList({
					id: this.storeId
				})
				this.cartList = data.data
			},
			// 获取店铺内 购物车 列表|
			async checkoutPrice() {
				let data = await storeCalculatePrice({
					id: this.storeId
				})
				this.cartMoneys = data.data
			},
			async checkoutStore() {
				let data = await orderStoreCheckout({
					id: this.storeId
				})
			},
			nav(id) {
				this.categoryId = id
				this.initss()
			},
			inits() {
				let token = uni.getStorageSync('token')

				let data = uni.getStorageSync('shopdetailsGoodsNav')
				this.isCreator = data.isCreator
				this.navList = data.productCategory
				this.storeId = data.store.id
				this.id = data.store.memberId
				this.pageable.pageNumber = 1
				this.stops = false
				this.goodsList = []

				if (token) {
					this.checkoutPrice()
					this.getCartList()
				}
				this.init()
			},
			initss() {
				this.pageable.pageNumber = 1
				this.stops = false
				this.goodsList = []
				this.init()
			},
			checkingAllGoods() {
				if (!this.stops) {
					this.init()
				}
			},
			async init() {
				let data = await productList({
					longitude: this.$store.state.longitude,
					latitude: this.$store.state.latitude,
					id: this.id,
					categoryId: this.categoryId,
					pageable: this.pageable
				})
				this.goodsList = [...this.goodsList, ...data.data]
				this.pageable.pageNumber++
				if (data.data.length != this.pageable.pageSize) {
					this.stops = true
				}
			},
			GoGoods(v) {
				this.$navigateTo('../GoodsDetails/GoodsDetails?id=' + v.id)
			},
			carShows() {
				if (this.cartMoneys.count != 0) {
					this.carShow = !this.carShow
				}
			}
		}
	}
</script>
<style lang="less" scoped>
	@import './index.less';
</style>
